home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1165_file_locking.rtf < prev    next >
Text File  |  1995-06-12  |  3KB  |  74 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f1\fmodern Courier;\f2\fmodern Ohlfs;}
  2. \paperw11760
  3. \paperh7200
  4. \margl120
  5. \margr120
  6. {\colortbl;\red0\green0\blue0;}
  7. \pard\tx1240\tx2480\tx3740\tx4980\tx6240\tx7480\tx8720\tx9980\tx11220\tx12480\f0\b0\i0\ulnone\fs28\fc1\cf1 Q: Why does 
  8. \f1 fcntl
  9. \fs24 (fd, F_SETLK, &lockstruct)
  10. \f0\fs28  give 
  11. \f1\fs24 EINVAL
  12. \f0\fs28 ?\
  13.  
  14. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0\cf0 \
  15. Q:  What is the preferred method of file locking?\
  16. \
  17. A:  The current release of  Sun's NFS file system has a number of problems regarding file locking.  Sun has implemented a "first crack" at solving the problems associated with extending Unix's file locking to NFS.  NeXT decided, upon looking at this, that there were too many hidden deadlock conditions inherent in the implementation to release.  Instead, the current version of 
  18. \f1\fs24\fc1\cf1 fcntl(2)
  19. \f0\fs28\fc0\cf0  will fail with 
  20. \f1\fs24\fc1\cf1 EINVAL
  21. \f0\fs28\fc0\cf0  if the directives 
  22. \f1\fs24\fc1\cf1 F_GETLK
  23. \f0\fs28\fc0\cf0  or 
  24. \f1\fs24\fc1\cf1 F_SETLK
  25. \f0\fs28\fc0\cf0  are used. These directives will work when a new version of NFS file locking is available,  in our 4.0 release.\
  26. \
  27. In the mean time, if a file locking system is necessary for your application, the best system to use is to write out a file whenever your application accesses a file for writing. The easiest thing to do is to create a file of the same name with an added extension, like "LCK" for instance.  Your application should check to see if this file exists before opening a file,  and create the lock file, if one doesn't exist. \
  28. \
  29. Here's one idea: if you open(2) a file with both CREATE and EXCLUSIVE turned on, then open(2) will return an error if the file exists, and will create the file if it doesn't.  Something like\
  30. \
  31.  
  32. \f1\fs24\fc1\cf1     #include <sys/file.h>\
  33.     int    fd;\
  34. \
  35.     fd = open(lockfile, O_CREAT | O_EXCL, 0600);\
  36.     \
  37.     if (fd < 0) \{                /* Something didn't work */\
  38.         if (errno != EEXIST) \{    /* Real error occurred */\
  39.             perror("Open failed");\
  40.             exit(1);\
  41.         \} \
  42.         else \{        /* Lock file is there     */\
  43.                 /* await lock release */\
  44.         \}\
  45.     \}\
  46.     else /* Things are OK */\
  47.  
  48. \f0\fs28\fc0\cf0 \
  49. This still has some problems when systems crash with files open, or the if the Workspace exits with your application still running.  To deal with this,  your application should erase these lock files (with the unlink(2) system call, for example) in an appDidTerminate: method. Also, when one of these lock files exist, you should display an alert panel asking if the user would like to override the lock.\
  50. \
  51. For applications that run on a single machine (without any network file access) you can use  
  52. \f1\fs24\fc1\cf1 flock(2)
  53. \f0\fs28\fc0\cf0 if you are concerned that your application may be run multiple times on the same machine.  This scheme works for network files too, when that support is added.\
  54. \
  55.  
  56. \b Note:
  57. \b0  the 
  58. \pard\tx1240\tx2480\tx3740\tx4980\tx6240\tx7480\tx8720\tx9980\tx11220\tx12480\f1\fs24\fc1\cf1 fcntl
  59. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28\fc0\cf0 (2) directives not associated with file locking: \
  60. \
  61.  
  62. \pard\tx1240\tx2480\tx3740\tx4980\tx6240\tx7480\tx8720\tx9980\tx11220\tx12480\f1\fs24\fc1\cf1     F_\{DUP,SET,GET\}FD\
  63.     F_\{GET,SET\}FL\
  64.     F_\{GET,SET\}OWN\
  65.  
  66. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28\fc0\cf0 \
  67. work fine.\
  68. \
  69. QA590\
  70. \
  71. Valid for 1.0, 2.0, 3.0, 3.1\
  72. \
  73.  
  74.